home *** CD-ROM | disk | FTP | other *** search
- Path: grimsel.zurich.ibm.com!usenet
- From: wgk@zurich.ibm.com (Keith Whittingham)
- Newsgroups: comp.lang.c++
- Subject: Re: How do I put DIFFERENT classes in the same linked list?
- Date: 8 Apr 1996 10:22:54 GMT
- Organization: IBM Research, ZRH
- Message-ID: <4kapdu$ocq@grimsel.zurich.ibm.com>
- References: <4ka938$bj6@wintermute.ecs.fullerton.edu>
- Reply-To: wgk@zurich.ibm.com
- NNTP-Posting-Host: pine.zurich.ibm.com
- X-Newsreader: IBM NewsReader/2 v1.00
-
- In <4ka938$bj6@wintermute.ecs.fullerton.edu>, grosin@titan (Gil Rosin) writes:
- >I'm trying to write a program where I need to maintain a linked list of
- >various items. Each item is a class, but each item is different from another
- >item. Let me give an example to clarify:
- >
-
- You're writing a heterogeneous collection class - a collection of mixed
- objects: the classic example is a fruitbowl which can contain apples,
- oranges, pears... In your case you have a library which can contain
- books, magazines, records, CDs or whatever. Contrast this with a
- bookshelf which can contain only books - a homogeneous collection class.
-
- The ingredients of the heterogeneous collection are:
-
- An (Abstract) base class
-
- Instance Classes to store in the collection derived from 'Base'
-
- A container class (such as a list, array etc.)
-
- The container class is, normally, also derived from the base class to
- so that you can have collections of collections - this is the next
- level of abstraction which you can leave for the moment.
-
- The base class describes the type of objects which can be stored:
-
- Classes
- =======
-
- Container: Bowl Library
-
- Base: Fruit Publication
-
- Instance: Apple, Orange,.. Book, Magazine,..
-
- Given that you can defined your classes, add the methods you need
- migrating methods that are common to all dervied (instance) classes to
- the base class. Here's a start for the fruit bowl example...
-
- enum FRUIT { APPLE, ORANGE /* etc. */ };
-
- class Fruit
- {
- public:
- virtual FRUIT Type(void) const = 0;
- int Weight(void) const;
- // ,,,
- };
-
- class Apple:
- public Fruit
- {
- public:
- FRUITTYPE Type(void) const = 0;
- int Crunchiness(void) const;
- };
-
- class Bowl
- {
- public:
- void Add(Fruit &);
- void Delete(Fruit &);
- int Count(void) const;
- // ...
- }
-
- Hope that helps...
-
- Keith
-
-
-